# 检查一个文件夹的哈希值，文件大小，文件名，生成哈希检查日志

import datetime
import os
import hashlib
import sys

import tkinter as tk
from tkinter import filedialog

def sum_hash(file_path):
    with open(file_path,"rb") as file:
        file_data=file.read()
        file_hash=hashlib.sha256(file_data).hexdigest()
        return file_hash

def calculate_size(file_path):
    file_size=os.path.getsize(file_path)
    return file_size

folder_path=filedialog.askdirectory()
folder_path+="/"

files_and_folders=os.listdir(folder_path)

folders =[]

files_info=[]

sum_depth=1

if(len(sys.argv) > 1):
    sum_depth=int(sys.argv[1].split("-")[1])

file_num=0
folder_num=0
for i in files_and_folders:
    if os.path.isdir(folder_path+i):
        folders.append(i)
        folder_num+=1
    else :
        file_num+=1
        files_info.append((i,sum_hash(folder_path+i),calculate_size(folder_path+i)))
         
with open(folder_path+f"哈希计算-{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}-共{file_num}项.txt","w+")as log:
    log.writelines(f"文件夹 {folder_path}\n")
    log.writelines("\n")
    for i in files_info:
        log.writelines(f"文件名    :{i[0]}\n")
        log.writelines(f"文件大小  :{i[2]}\n")
        log.writelines(f"文件哈希值:{i[1]}\n")
        log.writelines("\n")